package com.ruoyi.web.easytron.handler;

import com.ruoyi.web.easytron.util.TransactionUtil;
import org.tron.trident.proto.Chain;

import java.util.HashMap;
import java.util.Map;
public class TransferHandlerContext {

    private final HashMap<String, TransferHandler> handlers = new HashMap<>();

    public TransferHandlerContext() {
    }

    public TransferHandlerContext(Map<String, TransferHandler> handlers) {
        this.handlers.putAll(handlers);
    }

    public void addHandler(String name, TransferHandler handler) {
        handlers.put(name, handler);
    }

    public TransferHandler getHandler(String name) {
        return handlers.get(name);
    }

    public TransferHandler getHandler(Chain.Transaction.Contract.ContractType contractType) {
        return handlers.values().stream().filter(handler -> handler.supportContractType(contractType)).findFirst().orElse(null);
    }

    public TransferHandler getHandler(Chain.Transaction transaction) {
        Chain.Transaction.Contract.ContractType contractType = TransactionUtil.getFirstContractType(transaction);
        return handlers.values().stream().filter(handler -> handler.supportContractType(contractType)).findFirst().orElse(null);
    }

}

